<?php

namespace app\common\libary;

/**
 * 文件上传类库
 *
 * @author LCY <409028735@qq.com>
 */
class FileUpload
{

    public static function uploadImage(){

    }

    /**
     * 生成文件上传的模板，简化在页面中的代码编写
     * @param string $field 字段名称
     * @param string $files 默认的文件
     * @param integer $width 建议图片宽度
     * @param integer $height 建议图片高度
     * @param integer $crop   裁剪方式
     * @param string $type  类型   image或file
     * @param int $max_num  最多上传的文件数量
     * @param int $max_size 上传的单个文件的大小限制，单位MB
     * @return string 直接可用于页面展示的上传组件
     */
    public static function initUpload($field, $files = '', $width = 500, $height = 500, $crop = 0, $max_num = 1, $max_size = 2, $type = 'image')
    {
        $files = is_string($files) && !empty($files) ? explode(',', $files) : (array)$files;
        if ($max_num > 1 || count($files) > 1) {
            $multiple = true;
        } else {
            $multiple = false;
        }
        $title = $multiple ? '多' : '单';
        $name = $multiple ? $field . '[]' : $field;
        //过滤空的文件名
        $files = array_filter($files, function ($file) {
            if (empty($file)) {
                return false;
            }
            return true;
        });

        if ($type == 'image') {
            $title .= '图';
            $templates = '<div class="files" id="' . $field . '">
                       <span class="btn btn-sm btn-primary btn-upload-image" data-multiple="' . ($multiple ? 'true' : 'false') . '" data-max-num="' . $max_num . '" data-max-size="' . $max_size . '" data-width="' . $width . '" data-height="' . $height . '" data-crop="' . $crop . '"><i class="fas fa-image"></i>' . $title . '上传</span>
                                <div class="files-preview mt-2 d-flex flex-wrap ' . ($multiple ? 'draggable-zone' : '') . '">';
            if (!empty($files)) {
                foreach ($files as $file) {
                    if (empty($file)) {
                        continue;
                    }
                    $templates .= '<div class="card overlay w-125px h-125px me-3 mb-3 overflow-hidden bg-lighten  ' . ($multiple ? 'draggable' : '') . '">
                                        <div class="card-body p-0">
                                            <input type="hidden" name="' . $field . ($multiple ? '[]' : '') . '" value="' . $file . '">
                                            <div class="overlay-wrapper">
                                                <img src="' . $file . '" alt="" class="w-100"/>
                                            </div>
                                            <div class="overlay-layer bg-dark bg-opacity-25">
                                                <a href="' . $file . '" class="btn btn-primary btn-icon  btn-sm  img-link w-25px h-25px" title="查看大图"><i class="bi bi-eye"></i></a>
                                                <span class="btn btn-danger btn-icon  btn-sm ms-2 img-remove w-25px h-25px" data-field="' . $name . '" title="删除"><i class="bi bi-trash"></i></span>
                                                ' . ($multiple ? '<span class="btn btn-info btn-icon btn-sm ms-2 draggable-handle w-25px h-25px" title="拖拽排序"><i class="bi bi-arrows-move"></i></span>' : '') . '
                                            </div>
                                        </div>
                                    </div>';
                }
            } else {
                $templates .= '<input type="hidden" name="' . $name . '" value="" class="default-value" />';
            }


            return $templates . '</div></div>';
        } elseif ($type == 'file') {
            $title .= '文件';
            $templates = '<div class="files" id="' . $field . '">
                       <span class="btn btn-sm btn-primary btn-upload-file" data-multiple="' . ($multiple ? 'true' : 'false') . '" data-max-num="' . $max_num . '" data-max-size="' . $max_size . '" data-width="' . $width . '" data-height="' . $height . '" data-crop="' . $crop . '"><i class="fas fa-image"></i>' . $title . '上传</span>
                                <div class="files-preview mt-2 d-flex flex-wrap ' . ($multiple ? 'draggable-zone' : '') . '">';
            if (!empty($files)) {
                foreach ($files as $file) {
                    if (empty($file)) {
                        continue;
                    }
                    $templates .= '<div class="card overlay w-125px h-125px me-3 mb-3 overflow-hidden bg-lighten  ' . ($multiple ? 'draggable' : '') . '">
                                        <div class="card-body p-0">
                                            <input type="hidden" name="' . $field . ($multiple ? '[]' : '') . '" value="' . $file . '">
                                            <div class="overlay-wrapper">
                                                <span class="text-muted">' . $file . '</span>
                                            </div>
                                            <div class="overlay-layer bg-dark bg-opacity-25">
                                                <a href="' . $file . '" download="" class="btn btn-primary btn-icon  btn-sm w-25px h-25px" title="下载文件"><i class="bi bi-download"></i></a>
                                                <span class="btn btn-danger btn-icon  btn-sm ms-2 img-remove w-25px h-25px" data-field="' . $name . '" title="删除"><i class="bi bi-trash"></i></span>
                                                ' . ($multiple ? '<span class="btn btn-info btn-icon btn-sm ms-2 draggable-handle w-25px h-25px" title="拖拽排序"><i class="bi bi-arrows-move"></i></span>' : '') . '
                                            </div>
                                        </div>
                                    </div>';
                }
            } else {
                $templates .= '<input type="hidden" name="' . $name . '" value="" class="default-value" />';
            }


            return $templates . '</div></div>';
        }

        return '';
    }

    /**
     * 生成上传文件的图片预览
     *
     * @param array $images 图片集合
     * @param boolean $draggable 是否可以拖拽
     * @return string 可直接用于页面中的展示
     */
    public static function initPreviewImages($images = [], $draggable = false){
        if (is_string($images)) {
            $images = explode(',', $images);
        }
        $templates = '<div class="files-preview mt-2 d-flex flex-wrap ' . ($draggable ? 'draggable-zone' : '') . '">';
        foreach ($images as $img) {
            if (!$img) {
                continue;
            }
            $templates .= '<div class="card overlay w-125px h-125px me-3 mb-3 overflow-hidden bg-lighten  ' . ($draggable ? 'draggable' : '') . '">
                                            <div class="card-body p-0">
                                                <div class="overlay-wrapper">
                                                    <img src="' . $img . '" alt="" class="w-100"/>
                                                </div>
                                                <div class="overlay-layer bg-dark bg-opacity-25">
                                                    <a href="' . $img . '" class="btn btn-primary btn-icon  btn-sm  img-link"><i class="bi bi-eye"></i></a>
                                                    ' . ($draggable ? '<span class="btn btn-info btn-icon btn-sm ms-2 draggable-handle"><i class="bi bi-arrows-move"></i></span>' : '') . '
                                                </div>
                                            </div>
                                        </div>';
        }
    
        return $templates . '</div>';
    }

    /**
     * 生成上传文件的文件预览，可提供下载
     *
     * @return string
     */
    public static function initPreviewFiles($files = []){
        
    }
}
